#####################################################################
# Product:      KnowledgeEngine (Plone Edition)
#
# File:         AssessmentCleanUpAccessory.py
#
# Copyright:    Copyright (c) 2005, Faulkner Technologies
#
# Author:       Brian Oliver
#
# Description:  This file defines the AssessmentCleanUpAccessory class,
#               that provides a mechanism to erase all assessments
#               from a model.
#
# See Also:     Accessory, Model, Assessment, AssessmentStorageService
#####################################################################

#####################################################################
## KnowledgeEngine Library Imports
from Products.KnowledgeEngine.Common import *
from Products.KnowledgeEngine.Core import *
from Products.KnowledgeEngine.Accessory import *
from Products.KnowledgeEngine.PropertyDefinition import *
from Products.KnowledgeEngine.PropertyTypes import *
from Products.KnowledgeEngine.PropertyFormats import *
from Products.KnowledgeEngine.PropertyStructures import *
from Products.KnowledgeEngine.WizardStepDefinition import *

#####################################################################
## Zope Library Imports
from Globals import InitializeClass
from AccessControl import ClassSecurityInfo
from AccessControl.Role import RoleManager
 
#####################################################################
## Python Library Imports
# (none)

#####################################################################
## AssessmentCleanUpAccessory Class Definition
class AssessmentCleanUpAccessory(Accessory):
    """
        An interface specifying the standard features of an Accessory
    """

    #---------------------------------------------------------------#

    #define security
    security = ClassSecurityInfo()
    security.declareObjectPublic()
    security.setDefaultAccess("allow")
        
    #---------------------------------------------------------------#

    def getIdentity(self):
        """
            ABSTRACT: Return the unique identity of the accessory
            in the Knowledge Engine
        """
        
        #typically this will be the same as the class name
        return "AssessmentCleanUpAccessory"
        
    #---------------------------------------------------------------#
        
    def getTitle(self, genericObject):
        """
            Returns the title of the Accessory (when
            applied to the specified genericObject)
            
            RETURN TYPE: String
        """

        return "Delete All Assessments"
        
    #---------------------------------------------------------------#
        
    def getPurpose(self, genericObject):
        """
            ABSTRACT: Returns the purpose of the Accessory (when
            applied to the specified genericObject)
            
            RETURN TYPE: String
        """

        return "A simple tool to remove all assessments for this Model."
        
    #---------------------------------------------------------------#
        
    def getIconName(self, genericObject):
        """
            ABSTRACT: Returns the name of the Icon of the Accessory (when
            applied to the specified genericObject).
            
            The IconName represents the image file name that will be found using
            acquisition.
            
            RETURN TYPE: String
        """
        
        return "trash.png"
        
    #---------------------------------------------------------------#
        
    def getExecuteActionDisplayName(self, genericObject):
        """
            ABSTRACT: Returns the display name for the execute action of the Accessory (when
            applied to the specified genericObject)
            
            RETURN TYPE: String
        """

        #return different action names based on the locked status of the model
        return "Erase"
        
    #---------------------------------------------------------------#
        
    def isAvailableForGenericObject(self, genericObject):
        """
            Returns if the accessory is available for the 
            specified genericObject.
            
            This method is called to determine if an Accessory
            should be displayed for the specified genericObject 
            when rendering it's property editor etc.

            RETURN TYPE: Boolean
        """

        return genericObject.isInstanceOf("Model")
        
    #---------------------------------------------------------------#
        
    def getWizardStepDefinitions(self, context=None):
        """
            ABSTRACT: This method is used by this class to
            determine the actual wizard step definitions
            for the wizard
            
            RETURN: [WizardStepDefinition]
        """

        wizardStep1Definition = WizardStepDefinition("Delete All Assessment", "This accessory will delete ALL assessments in this Model.  This operation is not recoverable.  Are you sure you wish to do this?")

        return [wizardStep1Definition]

    #---------------------------------------------------------------#

    def getWizardStepFinishButtonTitle(self, wizardState, context=None):
        """
            Return the title of the finished button for the
            wizard step using the provided wizard state.
            
            PARAMS:
            wizardState - the current properties of the wizard in action
            context - usually a GenericObject instance that may 
                      be used to dynamically determine wizard
                      step definitions
            
            By default returns 'Finish...'.  Override this
            method if you'd like something else.

            RETURN: String
        """            
        
        return "Erase All Assessments (no undo)"
        
    #---------------------------------------------------------------#
        
    def executeAccessoryWith(self, genericObject, wizardState):
        """
            ABSTRACT: Executes the accessory for the specified
            genericObject, using the specified wizardState.            
            
            Returns a pair indicating the success of the accessory on
            the component and a corresponding message.
            
            RETURN TYPE: (Boolean, String)
        """
 
        #get the model
        model = genericObject

        #get the assessment storage service
        assessmentStorageService = model.getAssessmentStorageService()        
        
        #determine the total number of assessments
        totalNrAssessments = assessmentStorageService.getNrAssessmentsForModel(model)
 
        #delete assessments
        assessmentStorageService.deleteAssessmentsForModel(model)
 
        #always successful
        return (true, "Successfully Deleted %d Assessments" % totalNrAssessments)
        
    #---------------------------------------------------------------#

#####################################################################
## Class Initialisation

# Register Accessory Information with the Knowledge Engine
registerAccessory(AssessmentCleanUpAccessory())

#####################################################################
